package com.swy.config.tokenstrategy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import org.springframework.security.oauth2.provider.token.store.KeyStoreKeyFactory;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.security.KeyPair;

/**
 * @Author:Xujingjian
 * @时间: 2018/10/22
 * @描述:
 */
@Component
public class RedisTokenStrategy implements ITokenStrategy{


    @Autowired
    private RedisConnectionFactory connectionFactory;

    /**
     * tokenStore作用主要是新增、验证、刷新、修改token
     * @return
     */
    public TokenStore tokenStore() {
        //由于Redis版本兼容问题，无法使用
        return new RedisTokenStore(connectionFactory);
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

    }

    /**
     *  如果使用从数据库/缓存中查询验证，则资源服务器，需要配置
     *  security:
     *	  oauth2:
     *    	resource:
     *      	tokenInfoUri: http://localhost:8080/oauth/check_token
     *      	preferTokenInfo: true
     * @param endpoints
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
        endpoints.tokenStore(tokenStore());
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer oauthServer) {

    }

}
